package com.siwei.mes.sync.erp;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.bus.SyncGroup;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.erp.ContractService;
import com.siwei.mes.service.erp.CustomerService;
import com.siwei.mes.service.laboratory.ContractInfoService;
import com.siwei.mes.service.sync.erp.ErpProjectService;
import com.siwei.mes.service.system.ProjectAreaService;
import com.siwei.mes.sync.erp.entity.SyncProject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工程信息相关
 *
 * @author linzi
 * @since 2024-07-25 16:52:16
 */
@Api(value = "工程信息")
@RestController
@RequestMapping("erp/project")
public class SyncErpProjectController {
    private static final Logger log = LoggerFactory.getLogger("erp");
    @Value("${erp-token}")
    private String ERP_TOKEN;
    @Resource
    private ErpProjectService erpProjectService;
    @Resource
    private ContractInfoService contractInfoService;
    @Resource
    private ErpProjectMapper erpProjectMapper;
    @Resource
    private ContractService contractService;
    @Resource
    private CustomerService customerService;
    @Resource
    private ProjectAreaService projectAreaService;


    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(ERP_TOKEN), "token不正确");
    }

    @AnonymousAccess
    @ApiOperation(value = "新增或修改工程信息表")
    @PostMapping("/saveOrUpdate")
    public Json saveOrUpdate(@RequestBody @Validated(SyncGroup.class) SyncProject syncProject, HttpServletRequest request) {
        log.info("ERP新增或修改工程信息表，入参：{}", JSON.toJSONString(syncProject));
        log.info("ERP新增或修改工程信息已禁用，直接在ERP新任务单的时候处理");
        //this.checkToken(request);
        //
        ///**这里查询ERP合同信息*/
        //Contract contract = contractService.loadByCID(syncProject.getCID());
        //assertTrue(contract == null, "合同信息不存在");
        //
        ///**查询ERP客户信息*/
        //Customer customer = customerService.loadByCusID(contract.getCusid());
        //assertTrue(customer == null, "ERP客户信息不存在");
        //ErpProject erpProject = null;
        //try {
        //    ErpProject entity = new ErpProject();
        //    entity.setErpId(syncProject.getPROID());
        //    entity.setErpCustomId(customer.getCusid());
        //    entity.setErpContractId(contract.getCid());
        //    entity.setContractNo(contract.getContractno());
        //    entity.setProjectName(syncProject.getProjectName());
        //    entity.setProjectAddress(syncProject.getProjectAddress());
        //    entity.setLng(syncProject.getLng());
        //    entity.setLat(syncProject.getLat());
        //    entity.setSignDate(contract.getStartdate());
        //    entity.setContractType(0);
        //    entity.setLinkMan(syncProject.getProjectLinkMan());
        //    entity.setLinkTel(syncProject.getProjectLinkTel());
        //    entity.setProjectArea(syncProject.getProjectArea());
        //    entity.setCreateTime(new Date());
        //    entity.setUpdateTime(new Date());
        //    entity.setIsDel(0);
        //    erpProject = erpProjectService.saveOrUpdate(entity);
        //
        //    /**这里要新增工程区域*/
        //    projectAreaService.saveOrUpdate(syncProject.getProjectArea());
        //} catch (Exception e) {
        //    log.info("ERP新增或修改客户信息失败，错误原因：【{}】", e.getMessage(), e);
        //    /**手动回滚事务*/
        //    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        //    assertTrue("ERP新增或修改客户信息失败，错误原因：" + e.getMessage() + "】");
        //}
        //
        ///**这里操作协会的*/
        //String laboratoryId = contractInfoService.saveOrUpdate(erpProject, false);
        //if (StringUtils.isNotBlank(laboratoryId) && erpProject != null) {
        //    erpProjectMapper.updateByPrimaryKeySelective(new ErpProject().setId(erpProject.getId()).setLaboratoryId(laboratoryId).setUpdateTime(new Date()));
        //}
        return Json.succ().data(new ApiResult());
    }
}
